﻿using SuporeSliplampApp.Base;
using SuporeSliplampApp.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using Newtonsoft.Json;
using System.Collections.ObjectModel;

namespace SuporeSliplampApp.Views
{
    /// <summary>
    /// IDSetting.xaml 的交互逻辑
    /// </summary>
    public partial class IDSettingView : Window
    {
        public class IDSettings
        {
            public bool IsIDSwitchOn { get; set; }
            public string Patid { get; set; }
            public int PatidLength { get; set; }
        }

        private string patid;
        private string patLength;
        private bool isIDSwitchOn;


        public IDSettingView()
        {
            InitializeComponent();
            init();
        }


        private void init() 
        {
            LoadIDSettings();

            ID.Text = patid;
            IDlength.Text = patLength;
            iDSwitch.IsOn = isIDSwitchOn;
            
        }

        private void SaveIDSettings()
        {
            try
            {
                var iDSettings = new IDSettings();
                iDSettings.IsIDSwitchOn = iDSwitch.IsOn;
                if (iDSwitch.IsOn)
                {
                    iDSettings.Patid = ID.Text;
                    iDSettings.PatidLength = int.Parse(IDlength.Text);
                }
                else
                {
                    iDSettings.Patid = patid;
                    iDSettings.PatidLength = int.Parse(patLength);
                }

                string json = JsonConvert.SerializeObject(iDSettings, Formatting.Indented);
                File.WriteAllText("idsettings.json", json);
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void LoadIDSettings()
        {
            if(File.Exists("idsettings.json"))
            {
                string json = File.ReadAllText("idsettings.json");
                var idsettings = JsonConvert.DeserializeObject<IDSettings>(json);

                patid = idsettings.Patid;
                patLength = idsettings.PatidLength.ToString();
                isIDSwitchOn = idsettings.IsIDSwitchOn;

            }
        }



        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            SaveIDSettings();

            this.Close();

        }


        private void IDlength_TextChanged(object sender, TextChangedEventArgs e)
        {
            var textBox = (TextBox)sender;

            if (string.IsNullOrWhiteSpace(textBox.Text) || textBox.Text == "0")
            {
                textBox.Text = "1";
            }
            else
            {
                int number;
                bool isNumeric = int.TryParse(textBox.Text, NumberStyles.Integer, CultureInfo.InvariantCulture, out number);

                if (!isNumeric)
                {
                    // 如果输入的不是数字，设置为1
                    textBox.Text = "1";
                }
                else
                {
                    // 确保数字在1到10之间
                    if (number < 1)
                    {
                        textBox.Text = "1";
                    }
                    else if (number > 10)
                    {
                        textBox.Text = "10";
                    }
                }
            }


            textBox.CaretIndex = textBox.Text.Length;
        }

        private void IDAdd_Click(object sender, RoutedEventArgs e)
        {
            int currentNumber;
            if (int.TryParse(IDlength.Text, out currentNumber))
            {
                currentNumber = Math.Min(10, currentNumber + 1); 
                IDlength.Text = currentNumber.ToString();
            }
        }

        private void IDDec_Click(object sender, RoutedEventArgs e)
        {
            int currentNumber;
            if(int.TryParse(IDlength.Text, out currentNumber))
            {
                currentNumber = Math.Max(1, currentNumber - 1);
                IDlength.Text = currentNumber.ToString();
            }
        }





    }


}
